package dao;

import entity.Order;
import util.OrderUtil;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrderDao extends BaseDao {
    public OrderDao() {
        super();
    }

    // 保存订单
    public void save(Order order) {
        String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `add_id`, `payment`, `payment_type`, `post_fee`, `status`, `payment_time`, `consign_time`, `end_time`, `close_time`, `shipping_name`, `shipping_code`, `buyer_message`, `buyer_nick`, `buyer_rate`, `create_time`, `update_time`) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())";
        this.updateData(sql, order.getOrderId(), order.getUserId(), order.getAddId(), order.getPayment(), order.getPaymentType(), order.getPostFee(), order.getStatus(), order.getPaymentTime(), order.getConsignTime(), order.getEndTime(), order.getCloseTime(), order.getShippingName(), order.getShippingCode(), order.getBuyerMessage(), order.getBuyerNick(), order.getBuyerRate());
    }

    /**
     * 分页查询订单
     * 传入参数：用户id，当前页，每页显示的条数
     * 返回值：订单列表，以时间降序排列
     */
    public List<Order> queryByPage(int userId, int currentPage, int pageSize) {
        String sql = "SELECT * FROM `t_order` WHERE user_id = ? ORDER BY create_time DESC LIMIT ?, ?";
        List<Order> list = null;
        try (ResultSet rs = this.queryData(sql, userId, (currentPage - 1) * pageSize, pageSize)) {
            if (rs == null) {
                return null;
            }
            list = new ArrayList<>();
            while (rs.next()) {
                list.add(OrderUtil.rsToOrder(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 分页查询所有订单
     * 传入参数：当前页，每页显示的条数
     * 返回值：订单列表，以时间降序排列
     */
    public List<Order> queryByPageAll(int currentPage, int pageSize) {
        String sql = "SELECT * FROM `t_order` ORDER BY create_time DESC LIMIT ?, ?";
        List<Order> list = null;
        try (ResultSet rs = this.queryData(sql, (currentPage - 1) * pageSize, pageSize)) {
            if (rs == null) {
                return null;
            }
            list = new ArrayList<>();
            while (rs.next()) {
                list.add(OrderUtil.rsToOrder(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 查询订单总数
     */
    public int queryCount(int userId) {
        String sql = "SELECT COUNT(*) FROM `t_order` WHERE user_id = ?";
        int count = 0;
        try (ResultSet rs = this.queryData(sql, userId)) {
            if (rs == null) {
                return 0;
            }
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 查询所有订单总数
     */
    public int queryCountAll() {
        String sql = "SELECT COUNT(*) FROM `t_order`";
        int count = 0;
        try (ResultSet rs = this.queryData(sql)) {
            if (rs == null) {
                return 0;
            }
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }
}
